﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using Tebbee.UI.Client.ProductSer;

namespace Tebbee.UI.Client.Controllers
{
    public class AdminProductController : ApiController
    {
        private readonly ProductSerClient db = new ProductSerClient();

        //Product
        public HttpResponseMessage PostProduct(Tebbee.UI.Client.ProductSer.Product product)
        {
            if (ModelState.IsValid)
            {
                //db.ChannelFactory\(category);
                db.AddNewProduct(product);
                return Request.CreateResponse(HttpStatusCode.Created, product);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        public IEnumerable<Tebbee.UI.Client.ProductSer.Product> GetProducts(int id)
        {
            var queryString = HttpUtility.ParseQueryString(Request.RequestUri.Query);
            if (id < 1) id = 1;
            id = (id - 1) * 10;
            var tmp = db.ListProducts(id, queryString["value"]);
            return tmp;
        }

        public HttpResponseMessage DeleteProduct(int id)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (db.DeleteProduct(id) != 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, id);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found");
            }
        }

        public HttpResponseMessage PutProduct(Tebbee.UI.Client.ProductSer.Product product)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (db.EditProduct(product) != 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, product);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found");
            }
        }

        public HttpResponseMessage PostProductWithImg()
        {
            HttpResponseMessage result = null;
            var httpRequest = HttpContext.Current.Request;
            var fname = httpRequest.Form["fname"];
            if (httpRequest.Files.Count > 0)
            {
                var docfiles = new List<string>();

                foreach (string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];
                    var filePath = HttpContext.Current.Server.MapPath("~/Advertise/ProductImg/" + fname);

                    try
                    {
                        postedFile.SaveAs(filePath);
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                    }
                    docfiles.Add(filePath);
                }

                result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
            }
            else
            {
                result = Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            return result;
        }

    }
}
